
var app = getApp();
var city = require("../../../utils/city.js");
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data:{
    addressInfo : {}
  },
  address_id : '',
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
			key: 'HPNBZ-B426V-CZQPP-UN4R6-QYOF2-MYFU3'
		});
   var that = this;
   var address_id = options.address_id;
   if(address_id != 'undefined'){
      this.loadAddressInfo(address_id);
      this.address_id = address_id;
      that.consignee = this.data.addressInfo.name;
      that.tel = this.data.addressInfo.mobile;
      that.province = this.data.addressInfo.address.split(",")[0];
      that.city = this.data.addressInfo.address.split(",")[1];
      that.district = this.data.addressInfo.address.split(",")[2];
      that.address = this.data.addressInfo.address.split(",")[3];
      this.setData({ 
        addressInfo: {
          'objectId': address_id,
          'consignee': this.data.addressInfo.name,
          'tel': this.data.addressInfo.mobile,
          'province': this.data.addressInfo.address.split(",")[0],
          'city': this.data.addressInfo.address.split(",")[1],
          'district': this.data.addressInfo.address.split(",")[2],
          'address': this.data.addressInfo.address.split(",")[3]
        }
        })
   }   
   city.init(that);
  },
  loadAddressInfo : function(address_id){
    var addressList = wx.getStorageSync('addressList');
    for(var i=0;i<addressList.length;i++){
      if (addressList[i].objectId == address_id){
        this.setData({addressInfo:addressList[i]})
        break;
      }
    }
  },
  formSubmit : function(e){
    var consignee = e.detail.value.consignee,
      tel = e.detail.value.tel,
      province = this.data.city.selectedProvince,
      city = this.data.city.selectedCity,
      district = this.data.city.selectedDistrict,
      address = e.detail.value.address;
    var objectId = this.address_id;
    if(consignee == '' || tel == '' || province == '' || address == ''){
      wx.showToast({
        title : '请填写相关信息',
        duration : 1000,
        mask : true
      })
      return;
    }	
    console.log("consignee:" + consignee + ",tel:" + tel + ",province:" + province + ",city:" + city + ",district:" + district+ ",address:" + address + ",objectId:" + objectId);
    if(!/1[3-8]\d{9}/.test(tel)){
      wx.showToast({
        title : '请输入正确的手机号',
        duration : 1000,
        mask : true
      })
      return;
    }
    var page = this;
    var uid = wx.getStorageSync('uid');
    var access_token = wx.getStorageSync('access_token');
    
    var url = '';
    var data = {};
    var method = null;
    if (objectId != "" && null != objectId && 'undefined' != objectId && typeof (objectId) != "undefined"){
      url = 'http://core-dev.dzxwapp.com/parse/classes/ShipAddress/' + objectId;
      data = { 'name': consignee, 'mobile': tel, 'address': province + "," + city + "," + district + "," + address };
      method = 'PUT';
    } else {
      data = { 'name': consignee, 'mobile': tel, 'address': province + "," + city + "," + district + "," + address, 'dft': false };
      method = 'POST';
      url = 'http://core-dev.dzxwapp.com/parse/functions/createShipAddress';
    }
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'X-Parse-Application-Id':'jiKapGuqdTzWg62WgkDMKNUo',
        'X-Parse-Session-Token': access_token,
        'content-type': 'application/json'
      },
      success: function(res){
        console.log("createShipAddress:" + JSON.stringify(res));
        wx.redirectTo({
          url: '/pages/address/list/list'
        })
      },
      fail: function(res) {
        // fail
      }
    })
  },
  fetchPOI: function () {
    	var that = this;
    	// 调用接口
    	qqmapsdk.reverseGeocoder({
    		poi_options: 'policy=1',
    		get_poi: 1,
		    success: function(res) {
				console.log(res);
				// that.setData({
				// 	areaSelectedStr: res.result.address
				// });
		    },
		    fail: function(res) {
		//         console.log(res);
		    },
		    complete: function(res) {
		//         console.log(res);
		    }
    	});
    },
  backHome : function(e){
    app.backHome();
  }
})